import Vue from "vue";
import Vuex from "vuex";
import {reqAddOrUpdateShopCart, reqGoodsList} from "@/api";
import getUUID from "@/utail/get_uuid";
Vue.use(Vuex);

// 数据
let state = {
    goodsList:{}, //商品详情数据
    UUID:getUUID()
};

// 写好逻辑后，提交给mutations处理修改数据
let actions = {
    //获取产品的详细信息
    reqGoodsList(context,params){
        reqGoodsList(params).then(value =>{
            context.commit('REQGOODSLIST',value.data.data)
        })
    },
    // 将产品添加至购物车中
    async reqAddOrUpdateShopCart(context,{skuId,skuNum}){
        let result = await reqAddOrUpdateShopCart(skuId,skuNum);
        if (result.data.code === 200) {
            return 'ok';
        }else {
            return Promise.reject(new Error('false'));
        }


    },

};

// 只有mutations能够修改数据
let mutations = {
    REQGOODSLIST(state,value){
        state.goodsList = value;
    },

};

// 优化数据结构，过滤数据
let getters = {
    categoryView(state){
        return state.goodsList.categoryView || {};  //防止数据未请求回来时， return的值为 undefined
    },
    skuInfo(state){
        return state.goodsList.skuInfo || {};
    },
    spuSaleAttrList(state){
        return state.goodsList.spuSaleAttrList || {};
    },

};


export default {
    namespaced:true,
    state,
    mutations,
    actions,
    getters
}
